window.addEvent('domready', function(){
	var form = $('frmSearch');
	
	//pagination
	new Pagination($('search_table_pagination'),{
		onPageChange: function(obj){
			form.offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	//table header untuk sorting
	new SortableTableHeader($('search_table').getElement('tr[class:header]'), {
		tableFieldName:['perb_tgl_lapor','perb_tgl_kejadian','perb_tempat_kejadian','perb_keterangan','perb_status'],
		onHeaderClick:function(item, fieldName, sort){
			form.orderby.value = fieldName+" "+sort;
			search_cont(form);
		}
	});
	
	search(form);
});

function search(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('search_table_content').set('html', json.table_content);
			
			//rubah jumlah halamannya
			$('search_table_pagination').retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('search_table_content').set('html', json.table_content);
		}
	}).send();
}

function do_delete(rowElement, id){
	ajax(APP.base+'/ajax/delete/'+id, "post", "", $empty, function(res){
		var json = JSON.decode(res);
		if (!json.error){
			//tween opacity and destroy it
			rowElement.get('tween').chain(function(){
				rowElement.destroy();
			}).start('opacity',[1,0]);
		}
		FBModal_message(APP.base+'/ajax/messageDialog', "Delete", json.msg);
		search($('frmSearch'));
	});
}

function doUpdateStatus(id, status){
	ajax(APP.base+'/ajax/updateStatus/'+id+'/'+status, 'post', "", $empty, 
		function(res){
			search($('frmSearch'));
		});
}